package com.wanmait.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wanmait.movie.entity.Comments;
import com.wanmait.movie.entity.Movie;
import com.wanmait.movie.entity.User;
import com.wanmait.movie.entity.VO.CommentsVO;
import com.wanmait.movie.mapper.ActorMapper;
import com.wanmait.movie.mapper.CommentsMapper;
import com.wanmait.movie.service.CommentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;

    @Override
    public PageInfo<CommentsVO> getCommentsList(int pageNum, int movieId, String keyword) {

        PageHelper.startPage(pageNum, 10);
        MPJLambdaWrapper<Comments> wrapper=new MPJLambdaWrapper<Comments>()
                .selectAll(Comments.class)
                .select(User::getUsername)
                .leftJoin(User.class, User::getId, Comments::getUserId)
                .select(Movie::getMovieName)
                .leftJoin(Movie.class,Movie::getId,Comments::getMovieId);

        //LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByDesc(Comments::getScore);
        if (movieId != 0) {
            wrapper.eq(Comments::getMovieId, movieId);
        }
        if (keyword!=""){
            wrapper.like(Comments::getComment,keyword);
            wrapper.or().like(Movie::getMovieName,keyword);
        }
        List<CommentsVO> commentsList = commentsMapper.selectJoinList(CommentsVO.class,wrapper);
        return new PageInfo(commentsList);

    }

    @Override
    public List<Comments> getList() {
        return null;
    }

}
